//
//  JZHHouViewController.m
//  极致荟
//
//  Created by hetao on 14-5-15.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHHouViewController.h"
#import "JZHHouTableViewCell.h"

@interface JZHHouViewController ()
{
    NSMutableArray *dataArr;//网络数据
}
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UITextField *commenttext;

@end

@implementation JZHHouViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (JZHHouViewController *)initId:(id)objcetId type:(int)type
{
    JZHHouViewController *jzh;
    if(!jzh)
    {
        jzh=[[JZHHouViewController alloc]init];
    }
    if([objcetId isKindOfClass:[JZHCommodity class]])
    {
        jzh.com=objcetId;
        jzh.howtype=ComTyep;
    }else if(type==1)
    {
        jzh.act=objcetId;
        jzh.howtype=ActiviType;
    }else
    {
        jzh.act=objcetId;
        jzh.howtype=PartyType;
    }
    return jzh;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArr=[[NSMutableArray alloc]init];
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    self.navigationItem.title=self.name;
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    if(self.howtype==ActiviType || self.howtype==PartyType)
    {
        self.navigationItem.title=@"活动评论";
        [self.buttview setBackgroundColor:[UIColor colorWithRed:47/255.0 green:47/255.0 blue:47/255.0 alpha:1]];
    }else
        
    {
        self.navigationItem.title=@"商品评论";
        if(self.com.comtype==1)
        {
            [self.buttview setBackgroundColor:[UIColor colorWithRed:96/255.0 green:187/255.0 blue:57/255.0 alpha:1]];
        }
    }
    
    [self.commenttext setReturnKeyType:UIReturnKeyDone];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notkey:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [self httpDataList];
}

- (void)leftDrawerButtonPress:(UIButton *)but
{
    [UINavigationController Pop:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)notkey:(NSNotification *)not
{
    CGPoint bpoint=[[[not userInfo] objectForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];
    CGPoint epoint=[[[not userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
    [UIView animateWithDuration:0.3 animations:^{
    [self.view setCenter:CGPointMake(self.view.center.x, self.view.center.y+(epoint.y-bpoint.y))];
    }];
    
}

#pragma mark------text代理---
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self addpin];
    return YES;
}

#pragma mark------网络请求-------

///添加评论
- (void)addpin
{
    if(self.commenttext.text.length<5)
    {
        [MMProgressHUD showInformationWithTips:@"评论内容太短"];
        return;
    }
    [MMProgressHUD showWithStatus:@"提交评论中...."];
    NSNumber *commentid;
    NSString *commentType;
    NSLog(@"%d",self.com.comid);
    if(self.howtype==ComTyep)
    {
        commentid=[NSNumber numberWithInt:self.com.comid];
        commentType=@"topic_content";
    }else if(self.howtype==PartyType)
    {
        commentid=[NSNumber numberWithInt:self.act.actId];
        commentType=@"party";
    }else
    {
        commentid=[NSNumber numberWithInt:self.act.actId];
        commentType=@"club";
    }

    
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:commentType,@"commentType",[NSNumber numberWithInt:[User defuceUser].userId],@"userId",self.commenttext.text,@"content",commentid,@"commentId",[NSNumber numberWithInt:0],@"replyId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"comment/addComment") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            [MMProgressHUD dismissWithSuccess:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
            [MMProgressHUD dismissWithError:@"提交失败"];
        }
    }];
}
///获取用户评论内容相关列表
- (void)httpDataList
{
    NSNumber *commentid;
    NSString *commentType;
    if(self.howtype==ComTyep)
    {
        commentid=[NSNumber numberWithInt:self.com.comid];
        commentType=@"topic_content";
    }else if(self.howtype==PartyType)
    {
        commentid=[NSNumber numberWithInt:self.act.actId];
        commentType=@"party";
    }else
    {
        commentid=[NSNumber numberWithInt:self.act.actId];
        commentType=@"club";
    }
    
    
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:commentid,@"commentId",commentType,@"commentType", nil];
    
    [HttpRequest httpRequestPost:JZHHTTPURL(@"comment/commentList") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        
        if(rerurncode==0)
        {
            NSString *path=[[data objectForKey:@"data"] objectForKey:@"imgPrefix"];
            NSLog(@"%@",data);
            for(NSDictionary *comdic in  [[data objectForKey:@"data"] objectForKey:@"list"])
            {
                JZHReview *rev=[[JZHReview alloc]init];
                OtherUsers *ouser=[[OtherUsers alloc]init];
                
                ouser.userId=[[comdic objectForKey:@"userID"] intValue];
                ouser.username=[comdic objectForKey:@"userName"];
                ouser.userHeadImage=[path stringByAppendingString:[comdic objectForKey:@"userAvatar"]];
                rev.recUser=ouser;
                rev.comment=[comdic objectForKey:@"content"];
                rev.revdate=[NSDate dateWithTimeIntervalSince1970:[[comdic objectForKey:@"addTime"] longLongValue]];
                [dataArr addObject:rev];
            }
            [self.table reloadData];
        }else
        {
            
        }
    }];
}


#pragma mark ---tableview代理---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return dataArr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *str=@"adf";
    JZHHouTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(!cell)
    {
        cell=[[NSBundle mainBundle] loadNibNamed:@"JZHHouTableViewCell" owner:self options:nil][0];
        
    }
    [cell addCellData:dataArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

@end
